//
//  PersonalAboutViewController.m
//  suRead
//
//  Created by 龚浩植 on 15/11/12.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "PersonalAboutViewController.h"
#import "ServiceNews.h"
#import "UIViewController+HUD.h"

@interface PersonalAboutViewController ()<UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *aboutWebView;

/** 新闻标题 */
@property (nonatomic, strong) NSString *title;
/** 新闻内容 */
@property (nonatomic, strong) NSString *body;
@end

@implementation PersonalAboutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;
    
    self.aboutWebView.delegate = self;
    self.aboutWebView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
    self.aboutWebView.dataDetectorTypes = UIDataDetectorTypeNone;
    
//    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:url_net_about]];
//    [self.aboutWebView loadRequest:request];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [ServiceNews doFetchAboutUs:^(id responseObject, NSString *message)
    {
        self.title = [responseObject objectForKey:@"title"];
        self.body = [responseObject objectForKey:@"body"];
        [self showInWebView];
    }
                        failure:^(NSString *message)
    {
        [self showHUDText:message];
    }];
    
    
}
#pragma mark - ******************** 拼接html语言
- (void)showInWebView
{
    NSMutableString *html = [NSMutableString string];
    [html appendString:@"<html>"];
    [html appendString:@"<head>"];
    [html appendFormat:@"<link rel=\"stylesheet\" href=\"%@\">",[[NSBundle mainBundle] URLForResource:@"SXDetails.css" withExtension:nil]];
    [html appendString:@"</head>"];
    
    [html appendString:@"<body>"];
    [html appendString:[self touchBody]];
    [html appendString:@"</body>"];
    
    [html appendString:@"</html>"];
    
    
    NSString *jsString = [NSString stringWithFormat:@"<html> \n"
                          "<head> \n"
                          "<meta name=\"viewport\" content=\"width=device-width, initial-scale=0.8, minimum-scale=0.8, maximum-scale=0.8, user-scalable=no;\">"
                          "<style type=\"text/css\"> \n"
                          "a{text-decoration: none;\n}"
                          "body {font-size: %fpx;color: #505050;text-align: justify;text-justify:inter-ideograph;line-height:1.3em;}\n"
                          
                          "</style> \n"
                          "</head> \n"
                          "<body>%@</body> \n"
                          "</html>",  csssize,html];
    
    
    [self.aboutWebView loadHTMLString:jsString baseURL:nil];
    
    [theAppDelegate dismiss];
}

- (NSString *)touchBody
{
    NSMutableString *body = [NSMutableString string];
    [body appendFormat:@"<div class=\"title\">%@</div>",self.title];
//    [body appendFormat:@"<div class=\"time\">%@</div>",[theAppDelegate confromTimesp:self.detailModel.ptime :@"yyyy-MM-dd HH-mm"]];
    
    if (self.body != nil) {
        [body appendString:self.body =[self.body stringByReplacingOccurrencesOfString:@"<img" withString:@"<img width =\"100%\""]];
        
    }
    
    return body;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)goBake:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
